import requests
import csv



import csv

def save_to_csv(data, filename):
    """
    将数据保存到CSV文件中。

    参数:
    data -- 要保存的数据列表，每个元素是一个字典。
    filename -- CSV文件的名称，默认为'jobs.csv'。
    """
    # 确定字段顺序
    fieldnames = ['标题', '公司名称', '公司地址', '公司规模员工人数',
                  '公司行业', '公司福利', '薪资', '经验要求', '学历要求']

    # 打开文件以写入
    with open(filename, 'w', newline='', encoding='utf-8') as csvfile:
        writer = csv.DictWriter(csvfile, fieldnames=fieldnames)

        # 写入表头
        writer.writeheader()

        # 写入数据行
        for row in data:
            writer.writerow(row)
    csvfile.close()

def get_data(data):
    result = []
    for data1 in data:
        dic ={
            '标题' : data1['jobName'],
            '公司名称':data1['brandName'],
            '公司地址':data1['cityName']+data1['areaDistrict']+data1['businessDistrict'],
            '公司规模员工人数':data1['brandScaleName'],
            '公司行业':data1['brandIndustry'],
            '公司福利':data1['welfareList'],
            '薪资':data1['salaryDesc'],
            '经验要求':data1['skills'],
            '学历要求':data1['jobDegree']
        }
        result.append(dic)
    return result
    # 追加数据到CSV文件


cookies = {
    '__g': '-',
    'wd_guid': '395bc68a-a78a-465e-bc7a-dbbcc065777f',
    'historyState': 'state',
    '__fid': 'b05d590fa21d77d8a37097c132d549f7',
    'lastCity': '101040100',
    '__c': '1719481918',
    '__a': '31085588.1719481918..1719481918.5.1.5.5',
    'mp_8b1215dbcc852955566c4df66e547374_mixpanel': '%7B%22distinct_id%22%3A%20%22190591be7671cd0-0e8b5e4a3d0284-4c657b58-1bcab9-190591be768303e%22%2C%22%24device_id%22%3A%20%22190591be7671cd0-0e8b5e4a3d0284-4c657b58-1bcab9-190591be768303e%22%2C%22%24initial_referrer%22%3A%20%22https%3A%2F%2Fwww.zhipin.com%2Fweb%2Fcommon%2F403.html%3Fcode%3D31%22%2C%22%24initial_referring_domain%22%3A%20%22www.zhipin.com%22%7D',
    '__zp_stoken__': '8ed5fw43CrloeRhdsYRUYY2HDh8KJf8KCfMOOfGlvVcKMeE1jw4zCq8ODwrvDh1ldwqJiwpxpwppewp3CrcKXwq3CncK0wp93xIV3w7PDgcKww4jCo8SkxIXCksKnw4hRw4HCn0w1Fg0NGxIREhIcDRccERcWGxgYFhcVHh4QGUM4w47Dsj1ETkE1W1dcE1FuaVFqVRRjVFZHQmMQHG5COEFBR0bDiMKow4DDlsOMwqvDgMOYwr3Cp8OGKEE%2FRkLDgsKvPkhgDXMbZhZUDXoSw5Jrw4YNb8OCw6U1SUDDgsWETkwkSEJCQj1BSUJMNEEgw5Fdw4EYcMOCRi1CIkpMPUxAQkw9TkJMOD1MMzRMRjJOEA8cFRIvPcOCw4DDgcOrTD0%3D',
}



headers = {
    'accept': 'application/json, text/plain, */*',
    'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
    # 'cookie': '__g=-; wd_guid=db223967-9556-4b35-9e81-d56fd533c549; historyState=state; __c=1719478861; __a=96554898.1696755668.1719459940.1719478861.10.3.2.10; mp_8b1215dbcc852955566c4df66e547374_mixpanel=%7B%22distinct_id%22%3A%20%2219058ed3ef81da-089bc6b20f3e99-4c657b58-1bcab9-19058ed3ef92672%22%2C%22%24device_id%22%3A%20%2219058ed3ef81da-089bc6b20f3e99-4c657b58-1bcab9-19058ed3ef92672%22%2C%22%24initial_referrer%22%3A%20%22https%3A%2F%2Fwww.zhipin.com%2Fweb%2Fcommon%2Fsecurity-check.html%3Fseed%3DIetznNbAIF7azKAH0GefFSzBzff9cYb4OkJMUYy3Ovw%253D%26name%3D11f5a2fc%26ts%3D1719478825149%26callbackUrl%3Dhttps%253A%252F%252Fwww.zhipin.com%252Fweb%252Fgeek%252Fjob%253Fquery%253D%2525E6%252595%2525B0%2525E6%25258D%2525AE%2525E5%252588%252586%2525E6%25259E%252590%2525E5%2525B8%252588%2526city%253D101040100%2526page%253D1%22%2C%22%24initial_referring_domain%22%3A%20%22www.zhipin.com%22%7D; __zp_stoken__=8ed5fw5BsbBJLGGhnEQ1zVFXDhk9iT3hva8KLw4TDhVlwUMKHXsOFwq9mwrxmVsOLw4BwwpxRwqrCtsKZwqTCrFHDh2LDv2vCpcK9w7HDgsKnxKnEisKWwq3Cv8O1w4bCo0IxGxIRDR4cDRYSGRIbDREaFhccGBMQGRoeDU43w4rDvElJST0zT1JbD1diZFZuUxhuU1JBTm4PGGBOLUY9QUrCvcKnw4TDmMOAwqbCv8Ocw4vCo8OLJz1JSkfCvcKzMERlEm8NahtTEXQew5dsw4InesOOwrQ5P0zDh8WDSkIoPT1GTElMTkZCOEwvw41rw4UhdsOEwo85Rx1OQklBP0ZCSUM9SDZJPygwQko3SRQZEBANM0vDjsOFw4bDp0JJ',
    'priority': 'u=1, i',
    'referer': 'https://www.zhipin.com/web/geek/job?query=%E6%95%B0%E6%8D%AE%E5%88%86%E6%9E%90%E5%B8%88&city=101040100',
    'sec-ch-ua': '"Not/A)Brand";v="8", "Chromium";v="126", "Microsoft Edge";v="126"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
    'sec-fetch-dest': 'empty',
    'sec-fetch-mode': 'cors',
    'sec-fetch-site': 'same-origin',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36 Edg/126.0.0.0',
    'x-requested-with': 'XMLHttpRequest',
}

params = {
    'scene': '1',
    'query': input('输入职位名称:'),
    'city': '101040100',
    'experience': '',
    'payType': '',
    'partTime': '',
    'degree': '',
    'industry': '',
    'scale': '',
    'stage': '',
    'position': '',
    'jobType': '',
    'salary': '',
    'multiBusinessDistrict': '',
    'multiSubway': '',
    'page': '1',
    'pageSize': '30',
}
if __name__ == '__main__':
    # 创建CSV文件
    csv_file_path = './boss直聘.csv'
    response = requests.get('https://www.zhipin.com/wapi/zpgeek/search/joblist.json', params=params, cookies=cookies, headers=headers)
    # print(response.json())
    data =response.json()['zpData']['jobList']
    # print(data)
    list = get_data(data)
    save_to_csv(list, csv_file_path)
    print(list)